﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for UserAccount
/// </summary>
public class UserAccount
{
    public int MaKH { get; set; }
    public String UserName { get; set; }
    public String Pass { get; set; }
    public DateTime NgayTao { get; set; }
    public String HoKH { get; set; }
    public String TenKH { get; set; }
    public bool GioiTinh { get; set; }
    public DateTime NgaySinh { get; set; }
    public String DienThoai { get; set; }
    public String Email { get; set; }
    public String DiaChi { get; set; }
    public int Quyen { get; set; }
    public DateTime NgayDangNhapCuoi { get; set; }
    
    public  static void InsertAccount(UserAccount u)
    {
        String StoredProcedureName = "sp_InsertAccount";
        IDictionary<String, String> d = new Dictionary<String, String>();
        d.Add("@MaKH",u.MaKH.ToString());
        d.Add("@UserName", u.UserName);
        d.Add("@Pass", u.Pass);
        d.Add("@NgayTao", u.NgayTao.ToShortDateString());
        d.Add("@HoKH", u.HoKH);
        d.Add("@TenKH", u.TenKH);
        d.Add("@GioiTinh", u.GioiTinh.ToString());
        d.Add("@NgaySinh", u.NgaySinh.ToShortDateString());
        d.Add("@DienThoai", u.DienThoai);
        d.Add("@Email", u.Email);
        d.Add("@DiaChi", u.DiaChi);
        d.Add("@Quyen",u.Quyen.ToString());
        d.Add("@NgayDangNhapCuoi",u.NgayDangNhapCuoi.ToShortDateString());
        DataAccess.ExecuteNonQuery(StoredProcedureName, d);

    }

    public static void UpdateAccount(UserAccount u)
    {
        String StoredProcedureName = "sp_UpdateAccount";
        IDictionary<String, String> d = new Dictionary<String, String>();
        //d.Add("@MaKH", u.MaKH.ToString());
        d.Add("@UserName", u.UserName);
        d.Add("@Pass", u.Pass);
        //d.Add("@NgayTao", u.NgayTao.ToShortDateString());
        d.Add("@HoKH", u.HoKH);
        d.Add("@TenKH", u.TenKH);
        d.Add("@GioiTinh", u.GioiTinh.ToString());
        d.Add("@NgaySinh", u.NgaySinh.ToShortDateString());
        d.Add("@DienThoai", u.DienThoai);
        d.Add("@Email", u.Email);
        d.Add("@DiaChi", u.DiaChi);
        //d.Add("@Quyen", u.Quyen.ToString());
        d.Add("@NgayDangNhapCuoi", u.NgayDangNhapCuoi.ToShortDateString());
        DataAccess.ExecuteNonQuery(StoredProcedureName, d);
    }

    public static List<UserAccount> GetAllAccountList()
    {
        List<UserAccount> list = new List<UserAccount>();
        String StoredProcedureName = "sp_GetAllAccountList";
        String connectionString = DataAccess.ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand comd = new SqlCommand(StoredProcedureName, con);
        con.Open();
        SqlDataReader datareader = comd.ExecuteReader(CommandBehavior.CloseConnection);
        if (datareader.HasRows)
        {
            while (datareader.Read())
            {

                UserAccount u = new UserAccount()
                {
                    MaKH = int.Parse(datareader["MaKH"].ToString()),
                    UserName = datareader.GetString(1),
                    Pass = datareader.GetString(2),
                    NgayTao = DateTime.Parse(datareader["NgayTao"].ToString()),
                    HoKH = datareader.GetString(4),
                    TenKH = datareader.GetString(5),
                    GioiTinh = bool.Parse(datareader["GioiTinh"].ToString()),
                    NgaySinh = DateTime.Parse(datareader["NgaySinh"].ToString()),
                    DienThoai = datareader.GetString(8),
                    Email = datareader.GetString(9),
                    DiaChi = datareader.GetString(10),
                    Quyen = int.Parse(datareader["Quyen"].ToString()),
                    NgayDangNhapCuoi = DateTime.Parse(datareader["NgayDangNhapCuoi"].ToString())
                };
                list.Add(u);
            }
        }
        return list;
    }

    public static bool CheckLogin(String username, String password)
    {
        String StoredProcedureName = "sp_CheckLogin";
        IDictionary<String, String> d = new Dictionary<String, String>();        
        d.Add("@UserName", username);
        d.Add("@Pass", password);
        String result = (String) DataAccess.ExecuteScalar(StoredProcedureName, d);
        return (result != null);
    }
}
